<template>
  <div class="teachers-manage">
    <!--    分成两块不同的区域-->
    <el-card>
      <div class="desc-box">
        <div class="title">教师信息列表</div>
        <div class="desc">您可以对教师名称、手机号码、头像等信息进行添加或编辑操作，也可以对教师信息进行置顶哦！</div>
      </div>
      <div class="use-box">
        <el-button class="add-button" @click="openDialog(0)" type="primary">添加教师</el-button>
        <div class="search-box">
          <div class="search-input">
            <el-input placeholder="请输入姓名" v-model="queryInfo.Name"
                      class="input-with-select" clearable
                      @clear="getTableList">
            </el-input>
          </div>
          <div class="search-input">
            <el-input placeholder="请输入电话" v-model="queryInfo.Phone"
                      class="input-with-select" clearable
                      @clear="getTableList">
            </el-input>
          </div>
          <el-button class="search-button" @click="getTableList" type="primary">查询</el-button>
        </div>
      </div>
    </el-card>

    <el-card>
      <el-table
        max-height="530px"
        :data="tableData"
        border
        style="width: 100%"
        v-loading="loading"
        element-loading-text="拼命加载中......"
        :header-cell-style="{textAlign:'center'}"
        :cell-style="{textAlign:'center'}"
        :header-cell-class-name="setHeaderRowClassName"
      >
        <el-table-column type="index" fixed></el-table-column>
        <el-table-column prop="Name" label="教师姓名"></el-table-column>
        <el-table-column prop="Phone" label="手机号码"></el-table-column>
        <!--        教师头像-->
        <el-table-column prop="Image" label="教师头像">
          <el-image
            v-if="scope.row.Image"
            slot-scope="scope"
            :src="imageUrl+scope.row.Image"
            style="max-height: 100px; max-width: 100px"
            :preview-src-list="imageUrlList"
          >
          </el-image>
        </el-table-column>
        <el-table-column
          prop="Describe" show-overflow-tooltip label="教师描述"></el-table-column>
        <el-table-column label="置顶">
          <template slot-scope="scope">
            <el-button size="mini" type="primary"
                       plain @click="topClick(scope.row)">置顶
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" class="handle">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="openDialog(scope.row.ID)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteTeachers(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--    新增或编辑对话框-->
    <add-or-edit-dialog
      v-if="isShowDialogComponent"
      :title="title"
      :is-show-dialog-component="isShowDialogComponent"
      @dialogHandleClose="dialogHandleClose"
      :judge-id="judgeId"
    ></add-or-edit-dialog>
    <!--    分页组件-->
    <pagination :child-msg="queryInfo" @callFather="callFather"></pagination>

  </div>
</template>

<script>
//引入新增和编辑对话框
import AddOrEditDialog from './childComps/AddOrEditDialog'
//引入接口类
import TeachersManage from '../../model/teachersManage'
//引入分页组件
import Pagination from '../../components/base/pagination/Pagination'

export default {
  name: 'TeachersManage',
  components: {
    AddOrEditDialog,
    Pagination
  },
  created () {
    this.getTableList()
  },
  data () {
    return {
      tableData: [],
      //获取列表需要传入的信息
      queryInfo: {
        Name: '',
        Phone: '',
        Page: 1,
        Limit: 10,
        total: 0
      },
      //控制对话框组件的显示与隐藏
      isShowDialogComponent: false,
      //为对话框组件穿入title名称
      title: '',
      //给子组件传递的值，用于判断用户点击的是添加/编辑按钮
      judgeId: 0,
      //保存图片公共路径
      imageUrl: '',
      //用于头像的大图预览功能
      imageUrlList: [],
      loading: false

    }
  },
  methods: {
    //设置绑定学科的表头样式
    setHeaderRowClassName ({ rowIndex }) {
      if (rowIndex === 0) {
        return 'setHeaderSubjectColumnName'
      }
    },
    //分页回调
    callFather (value) {
      if (value.currentPage) this.queryInfo.Page = value.currentPage
      if (value.pageSize) this.queryInfo.Limit = value.pageSize
      this.getTableList()
    },
    //点击添加、编辑按钮
    openDialog (id) {
      //展开对话框组件
      this.isShowDialogComponent = true
      this.title = id === 0 ? '添加教师' : '编辑教师'
      this.judgeId = id === 0 ? 0 : id
    },
    //子组件中的对话框关闭发射出的方法
    dialogHandleClose (payload) {
      if (payload === 'submitButton') {
        this.getTableList()
      }
      this.isShowDialogComponent = false
    },
    //获取列表信息
    async getTableList () {
      try {
        this.loading = true
        const res = await TeachersManage.getTeachersList(this.queryInfo)
        this.loading = false
        this.tableData = res.ListData
        this.imageUrl = res.ImageUrl
        this.queryInfo.total = res.Total
        this.initImageUrlList()
      } catch (error) {
        this.loading = false
      }
    },
    //头像大图预览功能的初始化
    initImageUrlList () {
      this.tableData.forEach(item => {
        if (!item.Image) {
          //如果没有则return出去
          return
        } else {
          //如果有的话就把图片的地址存到定义的图片数组中
          this.imageUrlList.push(this.imageUrl + item.Image)
        }
      })
    },
    //点击删除按钮是触发
    async deleteTeachers (rowInfo) {
      const confirmResult = await this.$confirm('此操作将永久删除该教师, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除操作')
      } else {
        await TeachersManage.deleteTeachersInfo(rowInfo.ID)
        this.$message.success('成功删除该教师信息')
        await this.getTableList()
      }
    },
    //点击置顶按钮触发
    async topClick (rowInfo) {
      try {
        await TeachersManage.topTeachersInfo(rowInfo.ID)
        this.$message.success('该教师信息置顶成功')
        await this.getTableList()
      } catch (err) {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>
@import "../../../src/assets/style/realize/tableStyle.css";
.teachers-manage{
  background-color: #eee;
}
.el-card {
  margin-top: 20px;
  box-shadow: none;
}
.el-pagination {
  position: absolute;
  right: 0;
  padding: 30px;
}
/*给表头一行加背景颜色*/
/deep/ .setHeaderSubjectColumnName {
  background-color: #E6E6FA !important;
  opacity: 0.7 !important;
}

</style>
